﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicareModel;
using MedicareDataAccess;
using MedicareBusiness;

namespace MedicareWebUI
{
    public partial class EventManagement : System.Web.UI.Page
    {

        private void updateFields(int id)
        {
            PatientDAO patients = new PatientDAO();
            Patient aPatient = patients.getPatientByID(id);
            txtID.Text = Convert.ToString(id);
            txtName.Text = aPatient.name;
            txtLastName.Text = aPatient.lastName;
            txtSSN.Text = aPatient.ssn;
            txtDOB.Text = aPatient.dob.ToString("yyyy-MM-dd");
            txtAddress.Text = aPatient.address;
            txtPhone.Text = aPatient.phone;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnUpdate.Enabled = false;
            btnProcess.Enabled = false;
            txtID.Enabled = false;
            txtComments.Enabled = false;
            txtName.Enabled = false;
            txtLastName.Enabled = false;
            txtSSN.Enabled = false;
            txtDOB.Enabled = false;
            dropTriage.Enabled = false;
            btnEvent.Enabled = false;
            btnEnd.Enabled = false;
            gridResults.Visible = false;
            txtSearchDate.Visible = false;

            ValComments.Enabled = false;
            ValAddress.Enabled = false;
            ValPhone.Enabled = false; 
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Patient aPatient = new Patient();
            aPatient.idPatient = Convert.ToInt16(txtID.Text);
            aPatient.name = txtName.Text;
            aPatient.lastName = txtLastName.Text;
            aPatient.ssn = txtSSN.Text;
            aPatient.dob = Convert.ToDateTime(txtDOB.Text);
            aPatient.address = txtAddress.Text;
            aPatient.phone = txtPhone.Text;

            PatientBO patients = new PatientBO();
            patients.updatePatient(aPatient);
            gridPatients.DataBind();
            gridPatients2.DataBind();

        }


        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var id = (int)gridPatients.SelectedDataKey.Value;
            updateFields(id);
            btnUpdate.Enabled = true;
            btnProcess.Enabled = true;
            btnEvent.Enabled = true;

            ValAddress.Enabled = true;
            ValPhone.Enabled = true;
        }

        private void processRequest()
        {
        }

        protected void btnProcess_Click(object sender, EventArgs e)
        {
            Event anEvent = new Event();

            Patient aPatient = new Patient();
            aPatient.idPatient = Convert.ToInt16(txtID.Text);
            aPatient.name = txtName.Text;
            aPatient.lastName = txtLastName.Text;
            aPatient.ssn = txtSSN.Text;
            aPatient.dob = Convert.ToDateTime(txtDOB.Text);
            aPatient.address = txtAddress.Text;
            aPatient.phone = txtPhone.Text;
            anEvent.patient = aPatient;

            TriageDAO triages = new TriageDAO();
            anEvent.triage = triages.getTriageByID((int)dropTriage.SelectedIndex + 1); //Arreglo de negro para salir del paso. Preguntar. 

            anEvent.status = "Processed";
            anEvent.comments = txtComments.Text;

            EventBO events = new EventBO();
            events.createEvent(anEvent);

            PatientBO patients = new PatientBO();
            patients.processRequest(aPatient.idPatient);
            gridPatients.DataBind();
            gridPatients2.DataBind();
            ValComments.Enabled = false;
            ValPhone.Enabled = false;
            ValAddress.Enabled = false; 
        }

        protected void btnEMT_Click(object sender, EventArgs e)
        {
            Event anEvent = new Event();

            Patient aPatient = new Patient();
            aPatient.idPatient = Convert.ToInt16(txtID.Text);
            aPatient.name = txtName.Text;
            aPatient.lastName = txtLastName.Text;
            aPatient.ssn = txtSSN.Text;
            aPatient.dob = Convert.ToDateTime(txtDOB.Text);
            aPatient.address = txtAddress.Text;
            aPatient.phone = txtPhone.Text;
            anEvent.patient = aPatient;

            TriageDAO triages = new TriageDAO();
            anEvent.triage = triages.getTriageByID((int)dropTriage.SelectedIndex + 1); //Arreglo de negro para salir del paso. Preguntar. 

            anEvent.status = "Processed";
            anEvent.comments = txtComments.Text;

            EventBO events = new EventBO();
            events.createEvent(anEvent);

            PatientBO patients = new PatientBO();
            patients.processRequest(aPatient.idPatient);
            gridPatients.DataBind();
            gridPatients2.DataBind();
        }

        protected void btnEMT_Click1(object sender, EventArgs e)
        {
            Response.Redirect("EMTManagement.aspx");
        }

        protected void btnPatient_Click(object sender, EventArgs e)
        {
            Response.Redirect("PatientManagement.aspx");
        }

        protected void btnEnd_Click(object sender, EventArgs e)
        {
            EventDAO events = new EventDAO();
            PatientBO patients = new PatientBO();

            var id = (int)gridEvents.SelectedDataKey.Value;
            events.updateEventStatus(id, "Finished");
            lblSelectedEvent.Text = "Event marked as finished";

            var patientID = events.getEventByID(id).patient.idPatient;
            patients.resetPatient(patientID);

            gridEvents.DataBind();
            gridFinishedEvents.DataBind();
            gridPatients.DataBind();
            gridPatients2.DataBind();
            gridResults.DataBind(); 
        }

        protected void gridEvents_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblSelectedEvent.Text = "Selected Event ID:" + Convert.ToString(gridEvents.SelectedDataKey.Value);
            btnEnd.Enabled = true; 
        }

        protected void gridFinishedEvents_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnEvent_Click(object sender, EventArgs e)
        {

            Patient aPatient = new Patient();
            aPatient.idPatient = Convert.ToInt16(txtID.Text);
            aPatient.name = txtName.Text;
            aPatient.lastName = txtLastName.Text;
            aPatient.ssn = txtSSN.Text;
            aPatient.dob = Convert.ToDateTime(txtDOB.Text);
            aPatient.address = txtAddress.Text;
            aPatient.phone = txtPhone.Text;

            PatientBO patients = new PatientBO();
            patients.updatePatient(aPatient);
            gridPatients.DataBind();
            gridPatients2.DataBind();


            txtComments.Enabled = true;
            ValComments.Enabled = true;
            dropTriage.Enabled = true;
            btnProcess.Enabled = true;

            ValComments.Enabled = true; 
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "Date of Birth")
            {
                TextBox1.Visible = false;
                txtSearchDate.Visible = true;
            }

            else
            {
                TextBox1.Visible = true;
                txtSearchDate.Visible = false;
            }

        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            gridPatients.Visible = true;
            gridResults.Visible = false;
            DropDownList1.SelectedIndex = 0; 
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "Date of Birth")
            {
                TextBox1.Text = txtSearchDate.Text;
                gridPatients.Visible = false;
                gridResults.Visible = true;
                txtSearchDate.Visible = true;
                gridResults.DataBind();
            }

            else
            {
                gridPatients.Visible = false;
                gridResults.Visible = true;
                gridResults.DataBind();
            }
        }

        protected void gridResults_SelectedIndexChanged(object sender, EventArgs e)
        {
            var id = (int)gridResults.SelectedDataKey.Value;
            updateFields(id);
            btnUpdate.Enabled = true;
            btnProcess.Enabled = true;
            btnEvent.Enabled = true; 
            gridPatients.Visible = false;
            gridResults.Visible = true;

            ValAddress.Enabled = true;
            ValPhone.Enabled = true;
        }
    }
}